/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import dao.DBConnection;
import entity.Person;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

/**
 *
 * @author EKT
 */
public class Action extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        showPerson(request, response);
        //request.getRequestDispatcher("/ShowPerson.jsp").forward(request, response);

    }

    public void showPerson(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("username");
        System.out.println(name);
        String pass = request.getParameter("password");
        System.out.println(pass);

        try {
            Connection con = new DBConnection().getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from person where Username='"+name+"' and Password='"+pass+"'");

            List<Person> list = new ArrayList<Person>();
            while(rs.next()){
                Person person= new Person();
                person.setFullName(rs.getString("FullName"));
                person.setAge(rs.getInt("Age"));
                person.setAddress(rs.getString("Address"));
                person.setEmail(rs.getString("Email"));
                person.setPhone(rs.getString("Phone"));
                list.add(person);
            }
            request.setAttribute("list",list);
            System.out.println("List = " + list.size());

            if(list.size() >0 ){
                request.getRequestDispatcher("/ShowPerson.jsp").forward(request, response);
            }
            else
            {
                response.sendRedirect("Error.jsp");
            }



        } catch (Exception e) {
            System.out.println(e);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
